<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>For : Next</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="5">For : Next</font></b></p>

<br><b>语法</b><blockquote><pre><font face="Courier New, Courier, mono"size="2"><b><font color="#3A3966">For</font></b> &lt;variable&gt; = &lt;expression1&gt; <b><font color="#3A3966">To</font></b> &lt;expression2&gt; [<b><font color="#3A3966">Step</font></b> &lt;constant&gt;]
  ...
<b><font color="#3A3966">Next</font></b> [&lt;variable&gt;]
</font></pre></blockquote>
<b>概要</b><br><blockquote>


<b><font color="#3A3966">For</font></b> : <b><font color="#3A3966">Next</font></b> is used to create a loop within a program with the given parameters. At each loop the &lt;variable&gt; value is increased by a 1, (or of the "Step value" if a <b><font color="#3A3966">Step</font></b> value is specified) and when the &lt;variable&gt; value is above the &lt;expression2&gt; value, the loop stop. <br><br>With the <a href="break_continue.html">Break</a> command its possible to exit the <b><font color="#3A3966">For</font></b> : <b><font color="#3A3966">Next</font></b> loop at any moment, with the <a href="break_continue.html">Continue</a> command the end of the current iteration can be skipped. <br><br> <b><font color="#3A3966">For : Next</font></b> 循环制适用于整数值， 在  <b><font color="#3A3966">Step</font></b> 常数的表达式中.  <b><font color="#3A3966">Step</font></b> 常量也可以是负数. 

</blockquote><p><b>示例</b></p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">For</font></b> k = 0 <b><font color="#3A3966">To</font></b> 10 
    <b><font color="#3A3966">Debug</font></b> k
  <b><font color="#3A3966">Next</font></b>
</font></pre>

在此示例中，程序将循环 11次 (0 到 10)，然后退出。 

</blockquote><p><b>示例</b></p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">For</font></b> k = 10 <b><font color="#3A3966">To</font></b> 1 <b><font color="#3A3966">Step</font></b> -1
    <b><font color="#3A3966">Debug</font></b> k
  <b><font color="#3A3966">Next</font></b>
</font></pre>

在此示例中，该程序将循环 10 倍次(10 到 1 向后)，然后退出。 

</blockquote><p><b>示例</b></p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  a = 2
  b = 3 
  <b><font color="#3A3966">For</font></b> k = a+2 <b><font color="#3A3966">To</font></b> b+7 <b><font color="#3A3966">Step</font></b> 2
    <b><font color="#3A3966">Debug</font></b> k
  <b><font color="#3A3966">Next</font></b> k  
</font></pre>

Here, the program will loop 4 times before quitting, (k is increased by a value of 2 at each loop, so the k value is: 4-6-8-10). The "k" after the "Next" indicates that "Next" is ending the "For k" loop. 如果使用另一个变量，则编译器将生成一个错误。它可以用于嵌套"For/Next" 循环. 

</blockquote><p><b>示例</b></p><blockquote>


<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">For</font></b> x=0 <b><font color="#3A3966">To</font></b> 10 
    <b><font color="#3A3966">For</font></b> y=0 <b><font color="#3A3966">To</font></b> 5
      <b><font color="#3A3966">Debug</font></b> "x: " + x + " y: " + y
    <b><font color="#3A3966">Next</font></b> y
  <b><font color="#3A3966">Next</font></b> x
</font></pre>

<b>注意:</b> Be aware, that in PureBasic the value of &lt;expression2&gt; ('To' value) can also be changed inside the For : Next loop. 用错时这可能导致无限循环。 
</body></html>